package pers.mine.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import pers.mine.dto.PageInfo;
import pers.mine.dto.QueryHandler;
import pers.mine.po.Order;
import pers.mine.po.Ordergoods;
import pers.mine.service.inter.OrderService;
import pers.mine.service.inter.OrdergoodsService;
import pers.mine.utils.SimpleUtil;
import pers.mine.vo.Info;

/**
 * 后台订单 Controller
 * 
 * @author Mine
 * @since 2016年11月28日_下午10:29:09
 */
@Controller
@RequestMapping("/adminOrder")
public class AdminOrderController {
	private String pageTitle = "订单管理";
	private Info indexinfo = new Info();// 放到首页的信息
	@Autowired
	OrderService OrderService;
	@Autowired
	OrdergoodsService ordergoodsService;
	String onePath = "admin/order/";

	/**
	 * 依据分页信息,关键字信息 获得商品列表,并展示到后台商品页
	 */
	@RequestMapping(value = "/show")
	public String show(Integer nowPage, Integer pageSize, Integer pageCount,
			String keys, HttpServletRequest request) {
		QueryHandler qh = QueryHandler.creat();
		if (nowPage == null) {
			nowPage = 1;
		}
		if (pageSize == null) {
			pageSize = 5;
		}
		if (pageCount != null && nowPage != null && pageCount < nowPage) {
			nowPage = pageCount;
		}
		qh.limit(nowPage, pageSize);// 分页设置
		if (keys != null && !"".equals(keys.trim())) {
			// 关键字写回页面
			request.setAttribute("keys", keys);
			// 写死了。。。
			qh.andColumnLike("o_desc", keys).andColumnLike("o_id", keys)
					.andColumnLike("m_id", keys)
					.andColumnLike("o_address", keys);
		}
		// 排序方式
		qh.OrderByASC("o_id").set();
		// 依据筛选条件获取
		PageInfo<Order> pi = OrderService.findByQueryHandler(qh);
		// System.out.println(pi);
		// 判断是否可以放入信息
		Info temp = SimpleUtil.getIndexInfoWithRequest(request);
		if (temp != null) {
			indexinfo = temp;
		}
		if (indexinfo.isCanChange()) {
			indexinfo.setType(Info.SUCCESS);
			if (keys != null && !"".equals(keys.trim())) {
				indexinfo.setMsg("[订单查看]:对于关键字[" + keys + "]查询订单成功...");
			} else {
				indexinfo.setMsg("[订单查看]:查询所有订单成功...");
			}
		} else {
			indexinfo.setCanChange(true);
		}
		request.setAttribute("pageTitle", pageTitle);
		request.setAttribute("indexinfo", indexinfo);
		request.setAttribute("orderPageInfo", pi);
		return onePath + "orderPage";
	}

	/**
	 * 展示指定id订单 商品信息
	 */
	@RequestMapping(value = "/find/{id}")
	public String showOneOrderGoods(@PathVariable("id") Integer oid,
			Double cost, Model model) {
		// 查询
		List<Ordergoods> orderGoodsList = ordergoodsService.findByOrderId(oid);
		if (orderGoodsList != null) {
			indexinfo.setType(Info.SUCCESS);
			indexinfo.setMsg("[订单商品项查看]:以下是#" + oid + "号订单商品项信息...");
			model.addAttribute("indexinfo", indexinfo);
			model.addAttribute("orderGoodsList", orderGoodsList);
			model.addAttribute("orderId", oid);
			model.addAttribute("cost", cost);
			return onePath + "oneOrderGoods";
		}
		indexinfo.setType(Info.ERROR);
		indexinfo.setMsg("[订单商品项查看]:失败{id不正确?}...");
		indexinfo.setCanChange(false);
		model.addAttribute("indexinfo", indexinfo);
		return "forward:../show";
	}
}
